layui.use(['form','jquery','layer'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    setTimeout(getUserAuthority,100);
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index);
    })
    //保存设置按钮
    $('#saveSettings').click(function () {
        updateUserAuthority();
    });
    //查询用户所拥有的权限
    function getUserAuthority() {
        var userId = $('#userId').val();
        $.ajax({
            url: '/jsUiserAuthority/getUserAuthority',
            dataType: 'json',
            data: {userId: userId},
            success: function (data) {
                var unitType = [];
                var trId = data.data;
                //给复选框赋值
                for (var i = 0; i < trId.length; i++) {
                    switch (trId[i].authorityId) {
                        case 1 :
                            unitType.push('管理员')
                            break;
                        case 2 :
                            unitType.push('教师')
                            break;
                        case 3 :
                            unitType.push('运营')
                            break;
                        case 4 :
                            unitType.push('人事')
                            break;
                    }
                }
                for (var j = 0; j < unitType.length; j++) {
                    var unitTypeCheckbox = $("input[name='authorityId']");
                    for (var i = 0; i < unitTypeCheckbox.length; i++) {
                        if (unitTypeCheckbox[i].title == unitType[j]) {
                            unitTypeCheckbox[i].checked = true;
                        }
                    }
                }
                form.render();
            }
        });
    }
    function updateUserAuthority() {
        var userId = $('#userId').val();
        var checkboxs = $('#checkboxBox input');
        var checkboxValue = [];
        //获取选中复选框的值
        for (var i = 0; i < checkboxs.length; i++) {
            if(checkboxs[i].checked){
                checkboxValue.push(checkboxs[i].value);
            }
        }
        //更改权限
        $.ajax({
            url: '/jsUiserAuthority/updateUserAuthority',
            data: {
                userId: userId,
                authorityIds: checkboxValue,

            },
            type: 'post',
            dataType: 'json',
            success: function (data) {

                var layer = parent.layer;
                layer.close(layer.index);

            }
        });
    }
})